import store from '@/store/index.js'
import Request from '@/common/http/request.js'
const SignalRConnect = store.getters.SignalR_Connect; //获取SignalR全局对象
class CK_001 {
	constructor() {
		this.DeviceObj.initListener();
	}
	Link(connectId, callBackDeviceList, callBackLinkSuccess) { // 连接
		console.log(store.state.IPModel.SignalR_Status)
		if (!connectId) { //判断是否有设备ID
			//提供一个终端择列表，让用户选择其中一个
			this.DeviceObj.callBackDeviceList = callBackDeviceList;//监听重试连接需要的回调
			this.DeviceObj.startScan(callBackDeviceList);
		} else {
			this.DeviceObj.toConnect(connectId, callBackLinkSuccess);
		}
	}
	StopFind() { // 停止搜索
		this.DeviceObj.stopScan()
	}
	//获取状态，通过监听实时返回设备当前的状态
	//1、回调参数当前设备的状态
	GetStatus(callBackGetStatus) {
		//this.DeviceObj.callBackGetStatus = callBackGetStatus;
	}
	//停止设备连接，中断设备连接
	Disconnect(deviceId, callBackLinkDisConnect) { // 断开
		// console.log(deviceId)
		this.DeviceObj.disConnect(deviceId, callBackLinkDisConnect);
	}
	//获取数据的回调函数
	//回调实际返回的真实值
	CallBackGetData(callBackData) { // 返回获取的数据
		this.DeviceObj.callBackData = callBackData;
	}
	//写入函数，暂时保留
	WriteData() { // 写入数据
	}
	//获取串口PortName
	getPortName(deviceId, callBackPortNameList) {
		console.log(111)
		this.DeviceObj.startGetPortName(deviceId, callBackPortNameList);
	}

	//串口对象
	DeviceObj = {
		callBackDeviceList: null,
		callBackData: null,
		callBackGetStatus: null,
		curDeviceId: '',
		initListener() {
			var that = this;
			//监听重试连接
			SignalRConnect.on('onRetryConnect', function() {
				that.startScan(that.callBackDeviceList)
			})
			//监听数据回传
			SignalRConnect.on('onRS232DataCommunicate', function(data) {
				console.log(JSON.stringify(data));
				//data是什么格式还不清楚
				//data格式1 
				/* NUM 1,10.0097,10.0001,
				NUM 2,9.9933,9.9699,
				NUM 3,9.9933,9.9699,
				NUM 4,9.9933,9.9699,
				NUM 5,9.9933,9.9699,
				NUM 6,9.9933,9.9699,
				NUM 7,9.9933,9.9699,
				NUM 8,9.9933,9.9699,
				NUM 9,9.9933,9.9699,
				NUM 10,9.9933,9.9699,
				NUM 11,9.9933,9.9699,
				NUM 12,9.9933,9.9699,
				NUM 13,9.9933,9.9699,
				NUM 14,9.9933,9.9699,
				NUM 15,9.9933,9.9699,
				NUM 16,9.9933,9.9699,
				NUM 17,9.9933,9.9699,
				NUM 18,9.9933,9.9699,
				NUM 19,9.9933,9.9699,
				NUM 20,9.9933,9.9699,
				NUM 21,10.0000,10.0000,
				NUM 22,10.0000,10.0000,
				NUM 23,10.0000,10.0000,
				NUM 24,10.0000,10.0000, */
				//取最后一行数据
				// let originData = `${data}`;
				// let code = originData.split(/[(\r\n)\r\n]+/); // 根据换行或者回车进行识别
				// let list = []
				// code.forEach((item) => {
				// 	list.push(item.split(','))
				// })
				// console.log(list);
				// let end = list.splice(-1);
				// let Num1 = end[0][1];// 10.0000
				// let Num2 = end[0][2];// 10.0000

				//data格式2
				//1,32,00,0010.0000,0010.0000\r\n
				let NumsData = data.replace('\r\n', '').split(',');
				NumsData.forEach((item) => {
					item = item.replace(/^[0]+/, '')
				})
				//考虑到有多个通道   
				let endIndex = NumsData.length;
				let NumArray = [];
				for (let i = 3; i < NumsData.length; i++) {
					NumArray.push(NumsData[i].replace(/^[0]+/, ''));
				}
				//let Num1 = NumsData[3].replace(/^[0]+/, ''); // 10.0000
				//let Num2 = NumsData[4].replace(/^[0]+/, ''); // 10.0000
				console.log(JSON.stringify(NumArray))
				if (typeof(that.callBackData) == "function") {
					let obj = {
						Nums: NumArray.join(","),
						Model: data
					}
					console.log(JSON.stringify(obj))
					that.callBackData(obj);
				}
			})
		},
		//启动扫描
		startScan(callBackDeviceList) {
			var that = this;
			console.log(JSON.parse(Request.UserInfo()));
			console.log(JSON.parse(Request.UserInfo()).Business_Code);
			SignalRConnect.invoke("GetTerminal", JSON.parse(Request.UserInfo()).Business_Code).catch(function(
				err) {});
			//定义所有回调
			//获取所有终端列表
			SignalRConnect.on('onGetTerminal', function(terminalList) {
				console.log(JSON.stringify(terminalList)); // 终端列表
				//[{"enterNo":"006","clientName":"wwwww","connectID":"xi6kxJKyydXADLQA9oBnWQ"}]
				//返回的格式
				if (terminalList && terminalList.length > 0) {
					terminalList.forEach((item) => {
						item.deviceId = item.connectID;
					});
					if (typeof(callBackDeviceList) == "function") {
						callBackDeviceList(terminalList);
					}
				}

			})
			// let portName = store.getters.Serial_portName;
			// let BaudRate = store.getters.Serial_BaudRate;
			// let DataBits = store.getters.Serial_DataBits;
			// let StopBits = store.getters.Serial_StopBits;
			// let ParityBit = store.getters.Serial_ParityBit;
			// console.log(JSON.stringify(portName));
			// console.log(JSON.stringify(BaudRate));
			// console.log(JSON.stringify(DataBits));
			// console.log(JSON.stringify(StopBits));
			// console.log(JSON.stringify(ParityBit));
		},
		//停止扫描
		stopScan() {

		},
		//连接设备
		toConnect(deviceId, callBackLinkSuccess) {
			var that = this;
			that.curDeviceId = deviceId;
			//测试数据
			let portName = store.getters.Serial_portName;
			let BaudRate = store.getters.Serial_BaudRate;
			let DataBits = store.getters.Serial_DataBits;
			let StopBits = store.getters.Serial_StopBits;
			let ParityBit = store.getters.Serial_ParityBit;
			SignalRConnect.invoke("LinkRS232Terminal", deviceId, portName, BaudRate, DataBits, StopBits, ParityBit)
				.catch(function(err) {});
			SignalRConnect.on('onCallBackLinkRS232Terminal', function(isSuccess) {
				console.log(JSON.stringify(isSuccess));
				console.log(JSON.stringify(callBackLinkSuccess));
				/* {"status":"2500","message":"接口调用成功，将在onBLEConnectionStateChange触发连接结果"} */
				if (isSuccess) {
					let result = {};
					result.status = "2500";
					result.message = "连接成功！";
					if (typeof(callBackLinkSuccess) == "function") {
						callBackLinkSuccess(result);
					}
				}

			})


		},
		//断开连接
		disConnect(deviceId, callBackLinkDisConnect) {
			var that = this;
			console.log(JSON.stringify(deviceId))
			SignalRConnect.invoke("StopRS232Terminal", deviceId).catch(function(err) {});
			// SignalRConnect.on('onStopRS232Terminal', function() {
			// 	console.log(JSON.stringify('连接断开！'));
			// })
			SignalRConnect.on('onCallBackLinkRS232Terminal', function(isSuccess) {
				console.log(isSuccess)
				if (!isSuccess) {
					if (typeof(callBackLinkDisConnect) == "function") {
						callBackLinkDisConnect(isSuccess);
					}
				}
			})

		},
		startGetPortName(deviceId, callBackPortNameList) {
			console.log(deviceId);
			var that = this;
			SignalRConnect.invoke("GetTerminalPortNames", deviceId).catch(function(err) {});
			//传递终端串口列表
			SignalRConnect.on('onGetTerminalPortNames', function(listPortName) {
				//["COM4"]
				console.log(JSON.stringify(listPortName))
				if (listPortName && listPortName.length > 0) {
					let PortNameList = [];
					for (let i = 0; i < listPortName.length; i++) {
						PortNameList.push({
							value: i,
							text: listPortName[i],
						})
					}
					if (typeof(callBackPortNameList) == "function") {
						callBackPortNameList(PortNameList);
					}
				}
			})
		}

	}

}

export default CK_001